package com.ruoyi.project.user.ipdistribute.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.user.ipdistribute.domain.IpDistribute;
import com.ruoyi.project.user.ipdistribute.service.IIpDistributeService;

/**
 * ip分布Controller
 * 
 * @author shixiangwen
 * @date 2020-03-12
 */
@Controller
@RequestMapping("/user/ipdistribute")
public class IpDistributeController extends BaseController {
	private String prefix = "user/ipdistribute";

	@Autowired
	private IIpDistributeService ipDistributeService;

	@RequiresPermissions("user:ipdistribute:view")
	@GetMapping()
	public String ipdistribute(ModelMap mmap) {

		IpDistribute ipDistribute = new IpDistribute();
		ipDistribute.setDate(DateUtils.getDate());
		List<IpDistribute> list = ipDistributeService.selectIpDistributeList(ipDistribute);
		List<Map<String, Object>> datas = new ArrayList<>();
		list.forEach(item -> {
			Map<String, Object> map = new HashMap<>();
			map.put("name", item.getRegion());
			map.put("value", item.getTotal());
			datas.add(map);
		});
		mmap.put("datas", datas);

		return prefix + "/ipdistribute";
	}

	/**
	 * 查询ip分布列表
	 */
	@RequiresPermissions("user:ipdistribute:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(IpDistribute ipDistribute) {
		startPage();
		List<IpDistribute> list = ipDistributeService.selectIpDistributeList(ipDistribute);
		return getDataTable(list);
	}

	/**
	 * 导出ip分布列表
	 */
	@RequiresPermissions("user:ipdistribute:export")
	@PostMapping("/export")
	@ResponseBody
	public AjaxResult export(IpDistribute ipDistribute) {
		List<IpDistribute> list = ipDistributeService.selectIpDistributeList(ipDistribute);
		ExcelUtil<IpDistribute> util = new ExcelUtil<IpDistribute>(IpDistribute.class);
		return util.exportExcel(list, "ipdistribute");
	}

	/**
	 * 新增ip分布
	 */
	@GetMapping("/add")
	public String add() {
		return prefix + "/add";
	}

	/**
	 * 新增保存ip分布
	 */
	@RequiresPermissions("user:ipdistribute:add")
	@Log(title = "ip分布", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(IpDistribute ipDistribute) {
		return toAjax(ipDistributeService.insertIpDistribute(ipDistribute));
	}

	/**
	 * 修改ip分布
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Long id, ModelMap mmap) {
		IpDistribute ipDistribute = ipDistributeService.selectIpDistributeById(id);
		mmap.put("ipDistribute", ipDistribute);
		return prefix + "/edit";
	}

	/**
	 * 修改保存ip分布
	 */
	@RequiresPermissions("user:ipdistribute:edit")
	@Log(title = "ip分布", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(IpDistribute ipDistribute) {
		return toAjax(ipDistributeService.updateIpDistribute(ipDistribute));
	}

	/**
	 * 删除ip分布
	 */
	@RequiresPermissions("user:ipdistribute:remove")
	@Log(title = "ip分布", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		return toAjax(ipDistributeService.deleteIpDistributeByIds(ids));
	}
}
